package com.hzdy.master.service.impl;

import com.hzdy.master.entity.table.CreateTable;
import com.hzdy.master.repository.mysql.CreateTableRepository;
import com.hzdy.master.service.ICreateTableService;
import com.hzdy.master.utils.EntityUtils;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

@Service
public class CreateTableService implements ICreateTableService {

    @Autowired
    private CreateTableRepository createTableRepository;

    @Override
    public void create(Long id, String packageName, String storeName) throws IOException {
        CreateTable createTable = createTableRepository.findById(id).get();
        TypeSpec person = EntityUtils.build(createTable);
        JavaFile javaFile = JavaFile.builder(packageName, person)
                .build();
        Path path = Paths.get(storeName);
        javaFile.writeTo(path);
    }

    @Override
    public void createRepository(String packageName, String className,String storeName) throws IOException {
        TypeSpec repository = null;
        try {
            repository = EntityUtils.buildRepository(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        JavaFile javaFile = JavaFile.builder(packageName, repository)
                .build();
        Path path = Paths.get(storeName);
        javaFile.writeTo(path);
    }

    @Override
    public void compile(String location, String className) {
        EntityUtils.compile(location,className);
    }


}
